'''
Created on 05/10/2009

@author: Tony Allan
'''
from datastore import *
from runtime import rt
from runtime.spaces import *
from runtime.task import Task
from services import *
import time
import unittest

LOGGING = logging.WARNING
        
class DrivingTask(Task):
    ''' a user of HTTPTask '''
    def cb_start(self, method, uri, res, result, context, **kwargs):
        # add a new site
        res = Resource()
        http_uri = self.context.uri('tasks:').base.service.httptask
        server_uri = self.context.uri('local:')
        res.listen = server_uri
        self.log('add site %s' % res.listen)
        self.context.put(http_uri.listener.local, res)
        #
        # some sample data
        res_sample = Resource()
        res_sample.hello = 'World!'
        self.context.put(server_uri.sample.full(), res_sample)
        self.log('Test using - %s' % server_uri.sample.full_uri())

class HTTPTaskTest(unittest.TestCase):
    def setUp(self):
        pass
    
    def tearDown(self):
        pass

    def test_threads(self):
        '''[HTTPTask] test'''
        queue = Queue()
        spaces = Spaces()
        context = SpacesContext(spaces, queue)
        runtime = rt.Runtime()
        runtime.initialise_spaces_with_local_config_files(context)      
        # dummy task def resource
        task_def_resource = Resource()          
        #
        # start the http task
        taskt_uri = context.uri('tasks:').base.service.httptask
        taskt = HTTPTask(spaces, taskt_uri, task_def_resource)
        taskt.task_start()
        #
        # start the test driver
        time.sleep(1)
        taskd_uri = context.uri('tasks:').base.service.httptasktest
        taskd = DrivingTask(spaces, taskd_uri, task_def_resource)
        taskd.task_start()
        #
        time.sleep(5)
#        for u in context.dump():
#            print(str(u), str(context.get(u)))
        #
        taskt.task_stop()
        taskd.task_stop()
        time.sleep(1)

class RequestHandlerTest(unittest.TestCase):
    '''
    Test the RequestHandler defined in httptask module.
    '''

    def setUp(self):
        queue = Queue()
        self.spaces = Spaces()
        spaces_context = SpacesContext(self.spaces, queue)
        runtime = rt.Runtime()
        runtime.initialise_spaces_with_local_config_files(spaces_context)       
        uri_tasks = self.spaces.uri('tasks:')
        task_def_resource = Resource()
        self.task = HTTPTask(self.spaces, uri_tasks.base.service.httptask, task_def_resource)
        self.task.start() 
        # add a new site to test the http methods
        res = Resource()
        http_uri =spaces_context.uri('tasks:').base.service.httptask
        server_uri = spaces_context.uri('local:')
        res.listen = server_uri
        spaces_context.put(http_uri.listener.local, res)         

    def tearDown(self):
        self.task.task_stop()

    def test_do_put(self):
        '''test do_put. Test by putting a resource to spaces.'''
        queue = Queue()
        spaces_context = SpacesContext(self.spaces, queue) 
        sent_content = '123'
        res = Resource()
        res.content = sent_content
        conn = httplib.HTTPConnection('localhost:8000')
        conn.request(method="PUT", url='/dopost', body=res.serialise())
        response = conn.getresponse()
        response_content = response.read()
        conn.close()
        self.assertEqual(response_content, spaces_context.uri('local:').dopost)     
    
    def test_do_get(self):
        '''test do_get. Test by getting a resource from spaces.'''
        queue = Queue()
        spaces_context = SpacesContext(self.spaces, queue)        
        expected_content = '123'
        res = Resource()
        res.content = expected_content
        server_uri = spaces_context.uri('local:')
        # put a resource to spaces, so we can retrieve it.
        spaces_context.put(server_uri.doget, res)
        conn = httplib.HTTPConnection('localhost:8000')
        conn.request(method="GET", url='/doget')
        response = conn.getresponse()
        response_content = response.read()
        conn.close()
        self.assertEqual(response_content, expected_content)   
    
    def test_do_delete(self):
        '''test do_delete. Test by deleting a resource from spaces.'''
        queue = Queue()
        spaces_context = SpacesContext(self.spaces, queue)        
        expected_content = ''
        res = Resource()
        res.content = '123'
        server_uri = spaces_context.uri('local:')
        # put a resource to spaces, so we can retrieve it.
        spaces_context.put(server_uri.doget, res)
        conn = httplib.HTTPConnection('localhost:8000')
        conn.request(method="DELETE", url='/dodelete')
        response = conn.getresponse()
        response_content = response.read()
        conn.close()
        self.assertEqual(response_content, expected_content)
        # assert there is no resource at that location in spaces anymore
        self.assertEqual(None, spaces_context.get(server_uri.dodelete))
        
if __name__ == "__main__":
    unittest.main()